// 小数相加
export function accAdd(data1, data2) {
  var r1, r2, m, c;
  try {
    r1 = data1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = data2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  c = Math.abs(r1 - r2);
  m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
    var cm = Math.pow(10, c);
    if (r1 > r2) {
      data1 = Number(data1.toString().replace(".", ""));
      data2 = Number(data2.toString().replace(".", "")) * cm;
    } else {
      data1 = Number(data1.toString().replace(".", "")) * cm;
      data2 = Number(data2.toString().replace(".", ""));
    }
  } else {
    data1 = Number(data1.toString().replace(".", ""));
    data2 = Number(data2.toString().replace(".", ""));
  }
  return (data1 + data2) / m;
}
// 小数相减
export function numSub(data1, data2, precision) {
  let num, num1, num2;
  let precision1 = 0; // 精度
  try {
    num1 = data1.toString().split(".")[1].length;
  } catch (e) {
    num1 = 0;
  }
  try {
    num2 = data2.toString().split(".")[1].length;
  } catch (e) {
    num2 = 0;
  }
  num = Math.pow(10, Math.max(num1, num2));
  precision1 = precision || num1 >= num2 ? num1 : num2;
  return ((data1 * num - data2 * num) / num).toFixed(precision1);
}
// 小数相乘
export function accMulti(data1, data2) {
  var baseData = 0;
  try {
    if (data1?.toString().split(".")[1]) {
      baseData += data1.toString().split(".")[1].length;
    }
  } catch (e) {
    console.log(e);
  }
  try {
    if (data2?.toString().split(".")[1]) {
      baseData += data2.toString().split(".")[1].length;
    }
  } catch (e) {
    console.log(e);
  }
  if (data1 && data2) {
    return (
      (Number(data1.toString().replace(".", "")) *
        Number(data2.toString().replace(".", ""))) /
      Math.pow(10, baseData)
    );
  } else {
    return null;
  }
}
// 小数相除
export function division(arg1, arg2) {
  var t1 = 0,
    t2 = 0,
    r1,
    r2;
  try {
    if (arg1) {
      t1 = new String(arg1).split(".")[1].length;
    }
  } catch (e) {
    console.log(e);
  }
  try {
    if (arg2) {
      t2 = arg2.toString().split(".")[1].length;
    }
  } catch (e) {
    console.log(e);
  }
  if (arg1) {
    r1 = Number(arg1.toString().replace(".", ""));
  }
  if (arg2) {
    r2 = Number(arg2.toString().replace(".", ""));
  }
  //放大倍数后两个数相除 后，乘以两个小数位数长度相减后的10的次幂
  var newData = accMulti(r1 / r2, Math.pow(10, t2 - t1));
  //保留2个小数位数：return newData.toFixed(2)
  return newData;
}
